

//
//  CommentListViewController.m
//  DaJiang
//
//  Created by sally on 16/10/9.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CommentCell.h"
#import "CommentListViewController.h"
#import "ExpertModel.h"

@interface CommentListViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (assign, nonatomic) NSInteger pager;

@end

@implementation CommentListViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
  [self setRefresh];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"评价";
  [[CommonMethod sharedCommonMethod] setExtraCellLineHidden:self.tableView];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
}

- (void)setRefresh {
  __weak typeof(self) weakSelf = self;
  // 添加下拉刷新列表数据功能
  _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadRefreshData)];

  // 添加上拉加载更多列表数据功能
  _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadMoreData)];

  // 触发刷新
  [_tableView.header beginRefreshing];
}

#pragma mark - 获取数据
- (void)loadRefreshData {
  _pager = 1;
  [self loadData];
}

- (void)loadMoreData {
  _pager = _pager + 1;
  [self loadData];
}

- (void)loadData {
  self.tableView.footer.hidden = NO;
  NSInteger cursor = _pager > 1 ? [[self.tableData lastObject] commentid] : GCursorInit;

  [CommentModel queryCommentListWithProUid:self.expertId cursor:cursor completion:^(NSMutableArray *commentList) {
    if (commentList) {
      // 下拉刷新数据
      if (_pager == 1) {
        [self.tableData removeAllObjects];
        [self.tableData addObjectsFromArray:commentList];
        [self.tableView reloadData];

        if (commentList.count < GListCount) {
          self.tableView.footer.hidden = YES;
        }
      }
      // 上拉加载更多
      else {
        [self.tableData addObjectsFromArray:commentList];
        [self.tableView reloadData];
        if (commentList.count < GListCount) {
          self.tableView.footer.hidden = YES;
        }
      }

    } else {
      NSError *error = [CommentModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }

    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
  }];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return [CommentCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  CommentCell *cell = (CommentCell *) [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
  if (!cell) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"CommentCell" owner:nil options:nil] lastObject];
  }
  
  cell.cellData = [self.tableData objectAtIndex:indexPath.row];
  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
}

@end
